package com.example.content.repository.files

import com.example.core.network.body.ProgressResponseCallback
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import java.io.File
import java.net.URLConnection

/**
 * Created by zhudongao
 * Desc
 * Date 2020/12/6
 */
object FileConvert {
    fun guessMimeType(fileName: String): MediaType {
        val fileNameMap = URLConnection.getFileNameMap()
        val name = fileName.replace("#", "")
        var contentType = fileNameMap.getContentTypeFor(name)
        if (contentType == null) {
            contentType = "application/octet-stream"
        }
        return contentType.toMediaType()
    }

    fun fileWrappers(files: List<String>, callback:ProgressResponseCallback?):List<FileWrapper> {
        return files.map {
            val file = File(it)
            FileWrapper(file, file.name, guessMimeType(file.name), callback)
        }
    }
}